﻿using System;
using System.Collections.Generic;
using System.Data;
using CYZHAPI.Common.DB;
using CYZHAPI.Entity;
using CYZHAPI.IRepository;
using Microsoft.Extensions.Configuration;
using Oracle.ManagedDataAccess.Client;

namespace CYZHAPI.Repository
{
    public class RolRep : ReponsitoryBase<RolModel>, IRolRep
    {
        public RolRep(IDapperContext dbContext) : base(dbContext)
        {
        }

        public List<RolModel> get(string Userid, string pRolid)
        {
            List<RolModel> objList = new List<RolModel>();
            string sSql = "select a.userid,b.code, b.name,b.pcode,b.cmd from USER_MAP_ROL a,ALGORITHM_CODE b where a.rolid=b.code and a.userid=:userid and pcode=:pcode";
            OracleConnection objConn = this.GetReadingConnection();
            using (OracleCommand cmd = objConn.CreateCommand())
            {
                try
                {
                    cmd.CommandText = sSql;
                    // Assign id to the department number 50 
                    OracleParameter yhm = new OracleParameter("userid", Userid);
                    cmd.Parameters.Add(yhm);
                    OracleParameter mm = new OracleParameter("pcode", pRolid);
                    cmd.Parameters.Add(mm);
                    //cmd.BindByName = true;
                    DataSet dtTmp = new DataSet();

                    using(OracleDataAdapter dataAdapter = new OracleDataAdapter())
                    {
                        dataAdapter.SelectCommand = cmd;//检索command设置
                        dataAdapter.Fill(dtTmp);//检索结果保存在dtTmp数据集中
                        foreach(DataRow dr in dtTmp.Tables[0].Rows)   ///遍历所有的行
                        {//"select id, yhm, xm, mm, photo,email ,ustate from SYSUSER where yhm=:yhm, mm=:mm";
                            RolModel objRol=new RolModel();
                            objRol.userId = dr[0].ToString();
                            objRol.rolId = dr[1].ToString();
                            objRol.rolText = dr[2].ToString();
                            objRol.rolPid = dr[3].ToString();
                            objRol.cmdValue = dr[4].ToString();
                            objRol.rolValue = "";//config.GetSection(dr[1].ToString()).Value;
                            objList.Add(objRol);
                        }
                        
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return null;
                }
                 
            }
            return objList;
        }

        public List<RolModel> getRol(string userid, string pRolid)
        {
            throw new NotImplementedException();
        }
    }
}